package org.gdufe.auth.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.gdufe.auth.mapper.UserMapper;
import org.gdufe.auth.service.UserService;
import org.gdufe.pojos.dto.InsertUserDto;
import org.gdufe.pojos.pojo.User;
import org.gdufe.pojos.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public R selectByUsername(String username) {
        if (username==null||username.isEmpty()){
            return R.errorResult(500,"username格式错误");
        }
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        if (user==null){
            return R.errorResult(500,"查无此人");
        }
        return R.okResult(user);
    }

    @Override
    public R regist(InsertUserDto dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        if (username==null||username.isEmpty()||password==null||password.isEmpty()){
            return R.errorResult(500,"参数错误，请输入格式正确的用户名和密码");
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        String[] str={"normal"};
        String json = JSON.toJSONString(str);
        user.setAuthorities(json);
        System.out.println(json);
        int i = userMapper.insert(user);
        if (i<=0){
            return R.errorResult(500,"注册失败");
        }
        return R.okResult(200,"插入成功");
    }
}
